package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.NutritionPlanMapper;
import ym.pojo.NutritionPlan;
import ym.service.EnteralNutritionPlanService;
import ym.service.MealPlanService;
import ym.service.NutritionPlanService;
import ym.service.ParenteralNutritionPlanService;

import java.util.List;

@DubboService
public class NutritionPlanServiceImpl implements NutritionPlanService {
    @Autowired
    private NutritionPlanMapper nutritionPlanMapper;
    @Autowired
    private MealPlanService mealPlanService;
    @Autowired
    private EnteralNutritionPlanService enteralNutritionPlanService;
    @Autowired
    private ParenteralNutritionPlanService parenteralNutritionPlanService;

    @Override
    public void add(NutritionPlan nutritionPlan) {
        nutritionPlanMapper.insert(nutritionPlan);
    }

    @Override
    public void update(NutritionPlan nutritionPlan) {
        nutritionPlanMapper.updateById(nutritionPlan);
    }

    @Override
    public void delete(Integer id) {
        nutritionPlanMapper.deleteById(id);
        //删除膳食方案
        mealPlanService.deleteByNutritionPlanId(id);
        //删除肠内营养方案
        enteralNutritionPlanService.deleteByNutritionPlanId(id);
        //删除肠外营养方案
        parenteralNutritionPlanService.delete(id);
    }

    @Override
    public void deleteByPatientId(Integer patientId) {
        QueryWrapper<NutritionPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patientId", patientId);
        List<NutritionPlan> nutritionPlans = nutritionPlanMapper.selectList(queryWrapper);
        for (NutritionPlan nutritionPlan : nutritionPlans) {
            delete(nutritionPlan.getPatientId());
        }
    }

    @Override
    public NutritionPlan findById(Integer id) {
        return nutritionPlanMapper.selectById(id);
    }

    @Override
    public List<NutritionPlan> findAll() {
        return nutritionPlanMapper.selectList(null);
    }

    @Override
    public Page<NutritionPlan> search(String search, int page, int size) {
        QueryWrapper<NutritionPlan> queryWrapper = new QueryWrapper<>();
        if (search != null) {

        }
        return nutritionPlanMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public Page<NutritionPlan> searchByPatientId(Integer patientId, String search, int page, int size) {
        QueryWrapper<NutritionPlan> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("patientId", patientId);
        } else {
            queryWrapper.eq("patientId", patientId);
        }
        return nutritionPlanMapper.selectPage(new Page(page, size), queryWrapper);
    }

}

